<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	class moneymiljonair extends Plugins{
		private static $patterns = array(
			"/Welkom (.*?)</si",
			"/Je ontvangen e-mails van de afgelopen 7 dagen(.*?)<\/div>\n<\/div><\/div>/si",
			"/read_mail\/([a-z]*).png/i",
			"/php\?id=([0-9]*)/i",
			"/<div class=\"subject unread\">(.*?)<\/div>/i",
			"/http:\/\/www.moneymiljonair.nl\/am.php(.*?)\"/i",
			"/Uitloggen/i",
			"/unread.png/"
		);
		private static $plugin_name = "MoneyMiljonair";
		private static $cookie_name = "moneymiljonair.cookie";
		private static $cert_name = "moneymiljonair.crt";
		function init(){
			message(self::$plugin_name . " loaded","COMMENT");
			return true;
		}
		function action(){
			$counter = 0;
			$trigger = false;
			$once = false;
			$curl_options = array(
				CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name, 
				CURLOPT_FOLLOWLOCATION	=> true, // Read the cookie and skip HTTPS checkup.
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$login = self::getPage("http://www.moneymiljonair.nl/login/index.php", $curl_options);
			if(self::Parse(self::$patterns[0], $login, 1) == "Demo Demo"){
				message("Signed out!","ERROR");
				$once = false;
				$postfields = "login_username=".MONEYMIL_USER."&login_password=".MONEYMIL_PASS."&remember=true&redirect=/index.php";
				$curl_options = array(
					CURLOPT_POST 		=> true,
					CURLOPT_POSTFIELDS	=> $postfields,
					CURLOPT_COOKIEJAR	=> DATA_PATH.self::$cookie_name, // Write the cookie
					CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name,
					CURLOPT_RETURNTRANSFER	=> true,
					CURLOPT_FOLLOWLOCATION	=> true,
					CURLOPT_AUTOREFERER	=> true,
					CURLOPT_MAXREDIRS	=> 10,
					CURLOPT_SSL_VERIFYHOST => 0,
					CURLOPT_SSL_VERIFYPEER => 0,
					CURLOPT_REFERER		=> "https://www.moneymiljonair.nl/index.php"
				);
				$login = self::getPage("https://www.moneymiljonair.nl/login.php", $curl_options);
			}
			if(self::Parse(self::$patterns[6],$login,0) == "Uitloggen" && $once == false){
				message("Succesfully signed in as ".self::Parse(self::$patterns[0], $login, 1).".","NOTICE");
				$once = true;
			}
			/* Now we are logged in */
			
			$curl_options = array(
				CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name
			);
			$content = self::getPage("http://www.moneymiljonair.nl/login/read_mail.php",$curl_options);
			$unreaded_mails = self::Parse(self::$patterns[1],$content,1);
			$links = explode("href",$unreaded_mails);
			foreach($links as $k => $l){
				if(self::Parse(self::$patterns[2],$l,1) == "unread"){
					$mail_id = self::Parse(self::$patterns[3],$l,1);
					$mail_title = self::Parse(self::$patterns[4],$links[$k-1],1);
					if(self::Parse(self::$patterns[7],$l,0) == "unread.png"){
						$curl_options = array(
							CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name
						);
						$content = self::getPage("http://www.moneymiljonair.nl/login/read_mail.php?id=".$mail_id,$curl_options);
						$final_link = self::Parse(self::$patterns[5],$content,1);
						$final_link = "http://www.moneymiljonair.nl/am.php".$final_link;
						
						$curl_options = array(
							CURLOPT_FOLLOWLOCATION	=> true,
							CURLOPT_AUTOREFERER	=> true,
							CURLOPT_MAXREDIRS	=> 10,
							CURLOPT_HEADER		=> true,
							CURLOPT_SSL_VERIFYHOST => 0,
							CURLOPT_SSL_VERIFYPEER => 0
						);
						$content = self::getPage($final_link,$curl_options);
						
						$counter++;
						message($counter." Read email: ".$mail_id." ".$mail_title,"COMMENT");
						$buffer[] = $mail_id;
						sleep(15);
						$trigger = true;
					}
				}
			}
			if($trigger == false){
				// No new mails
				message("No new emails.", "NOTICE");
			}
			$trigger = false;
			return true;
		}
	}
	
?>
